﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.DataAccess;
using WLFramework.Json;
using WLFramework.Validation;
using WLFramework.Web;

namespace SampleSite.admin.ajax
{
    using GenericHtml;

    /// <summary>
    /// getmenus 的摘要说明
    /// </summary>
    public class getmenus : AjaxHttpHandler
    {
        private class InputData
        {
            [Required]
            public string AppId { get; set; }
        }

        public override void OnProcessRequest(HttpContext context)
        {
            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count > 0)
            {
                throw new Exception("输入的数据格式不正确");
            }

            int appid = int.Parse(WLFramework.Security.AES.AESDecrypt(input.AppId));
            if (appid < 0)
            {
                throw new Exception("参数错误");
            }

            using (PermissionContext db = new PermissionContext())
            {
           
                var app = from p in db.sys_Applications
                          where p.A_ID == appid && p.A_State == 1
                          select p;
                if (app.Count() != 1)
                    throw new Exception("参数错误");


                var roles = (from p in db.sys_UserPosition
                           join r in db.sys_PostRoles on p.P_ID equals r.P_ID
                           where p.U_ID == this.LoginUser.U_ID &&
                                 p.P_State == 1 && r.R_State == 1
                           select r.R_ID).Union(from f in db.sys_UserRoles
                            where f.U_ID == this.LoginUser.U_ID &&
                                  f.R_State == 1
                            select f.R_ID);
                
                var menus = (from p in db.sys_RoleModule
                             where p.M_State == 1 &&
                                   roles.Contains(p.R_ID) 
                             select p).Distinct();
                
                var query = from p in db.sys_ModuleList
                            join r in menus on p.M_ID equals r.M_ID
                                into temp
                            from t in temp.DefaultIfEmpty()
                            where p.M_State == 1 &&
                                  p.M_AppID == appid &&
                                  ((p.M_ParentID == 0) || (t != null))
                            select new
                            {
                                p.M_ID,
                                p.M_CName,
                                p.M_ParentID,
                                p.M_MoudleFormUrl
                            };

                var modules = query.Distinct().ToList();
                
                var list = from m in modules
                           where m.M_ParentID == 0
                           select new
                           {
                               GroupName = m.M_CName,
                               MenuItems = (from p in modules
                                            where p.M_ParentID == m.M_ID
                                            select new
                                            {
                                                MenuName = p.M_CName,
                                                Url = p.M_MoudleFormUrl
                                            }).ToList()
                           };

                list = list.Where(k => k.MenuItems != null && k.MenuItems.Count > 0).ToList();

                context.Response.Write(JSON.Instance.ToJSON(list));
            }
        }
    }
}